<?php
namespace Systemht\Controller;
use Think\Controller;
class LoginController extends Controller{
	public function index(){
		layout(false);
		if(IS_POST){
			$username = I('post.username');
			$password = I('post.password');
			if(empty($username)){
				$this->ajaxReturn('请输入用户名');
			}
			if(empty($password)){
				$this->ajaxReturn('请输入密码');
			}
			$model = M('Admin');
			$info = $model->where(array('username' => $username))->find();
			if($info){
				if($info['password'] != md5($password)){
					$this->ajaxReturn('用户名或密码错误');
				}
				session('uname_admin',$info['username']);
				session('uid_admin',$info['id']);
				$data['last_time'] = time();
				$data['last_ip'] = get_client_ip();
				$model->where(array('id'=>$info['id']))->save($data);
				$this->ajaxReturn('OK');
			}
			else{
				$this->ajaxReturn('该用户名不存在');
			}
		}
		$this->display('Login/login');
	}
	public function _initialize(){
		$uname = session('uname_admin');
		if(!empty($uname)){
			$this->redirect('Index/index');
		}
	}
}
